package org.javabasics.classes;

import java.awt.*;

/**
 * Created by LUOLIANG on 2016/9/23.
 * 抽象类的用法练习
 */
public abstract class Shape {
    {
        System.out.println("执行Shape抽象类的初始化块...");
    }

    private String color;

    //定义一个计算周长的抽象方法
    public abstract double calPerimeter();

    //定义一个返回形状的抽象方法
    public abstract String getType();

    //定义构造器，抽象类的构造器不能用于创建对象，只能供子类调用
    public Shape() {
    }

    public Shape(String color) {
        System.out.println("执行Shape的构造器...");
        this.color = color;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

class Triangle extends Shape {
    //定义三角形的三边
    private double a;
    private double b;
    private double c;

    public Triangle(String color, double a, double b, double c) {
        super(color);
        this.setSides(a, b, c);
    }

    public void setSides(double a, double b, double c) {
        if (a + b <= c || a + c <= b || b + c <= a) {
            System.out.println("三角形的两边之和必须大于第三边");
            return;
        }
        this.a = a;
        this.b = b;
        this.c = c;
    }

    //重写抽象类的计算周长方法和获取形状方法
    @Override
    public double calPerimeter() {
        return a + b + c;
    }

    @Override
    public String getType() {
        return "三角形";
    }
}

class Circle extends Shape {
    //定义圆的半径
    private double radius;

    public Circle() {
    }

    public Circle(String color, double radius) {
        super(color);
        this.radius = radius;
    }

    @Override
    public double calPerimeter() {

        return Math.PI * 2 * radius;
    }

    @Override
    public String getType() {
        return "圆形";
    }

    public static void main(String[] args) {
        Shape triangle = new Triangle("黑色", 3, 4, 5);
        Shape circle = new Circle("红色", 3);
        System.out.println(triangle.getType());
        System.out.println(triangle.calPerimeter());
        System.out.println(triangle.getColor());
        System.out.println(circle.getType());
        System.out.println(circle.calPerimeter());
        System.out.println(circle.getColor());
    }
}